﻿namespace SEED.SEEDModels {
    using System;
    using System.IO;
    using System.Collections.Generic;

    public static class Helper {
        
        public static List<LogicalRecordIndentBlock> LogicalRecordIndentBlocks = new List<LogicalRecordIndentBlock>();

        public static int GetVariavleCharArrayLength(BinaryReader binReader, int remainSpace) {
            string tempStr = "";
            // 注意，有可能在进入此函数时，刚好remainSpace == 0；
            if (remainSpace != 0) {
                while (binReader.PeekChar() != '~') {
                    --remainSpace;
                    tempStr += binReader.ReadChar();
                    if (remainSpace == 0) {
                        //跳过逻辑记录标识块
                        binReader.BaseStream.Position += 8;
                        break;
                    }
                }

                if (remainSpace == 0) {
                    string tempStr2 = "";
                    while (binReader.PeekChar() != '~') {
                        tempStr2 += binReader.ReadChar();
                    }
                    tempStr += tempStr2;
                    //把跳过的位置再还原回去
                    binReader.BaseStream.Position -= 8;
                }
            }
            else { // 进入此函数时,remainSpace == 0
                binReader.BaseStream.Position += 8;
                while (binReader.PeekChar() != '~') {
                    tempStr += binReader.ReadChar();
                }
                //把跳过的位置再还原回去
                binReader.BaseStream.Position -= 8;
            }
            int length = tempStr.Length;
            binReader.BaseStream.Position -= length;

            return (length + 1);// 包括结束符号"~"
        }

        public static string GetStrFromItAcrossLogicalRecdIndentBlock(BinaryReader binReader, int remainSpace) {
            string tempStr = new string(binReader.ReadChars(remainSpace));
            SetLogicalRecordIndentBlock(binReader);
            while (binReader.PeekChar() != '~') {
                tempStr += binReader.ReadChar();
            } 
            binReader.BaseStream.Position += 1;//跳过用于表示时间结束的"~"

            return tempStr;
        }

        public static string GetSStrFromItAcrossLogicalRecdIndentBlock(BinaryReader binReader, int remainSpace, int length) {
            char[] charsInRemainSpace = binReader.ReadChars(remainSpace);
            Helper.SetLogicalRecordIndentBlock(binReader);
            char[] alseNeededChars = binReader.ReadChars(length - remainSpace);
            string str = new string(charsInRemainSpace);
            str += new string(alseNeededChars);

            return str;
        }

        public static float GetFloatFromItAcrossLogicalRecdIndentBlock(BinaryReader binReader, int remainSpace, int length) {
            string str = GetSStrFromItAcrossLogicalRecdIndentBlock(binReader, remainSpace, length);
            return float.Parse(str);
        }

        public static int GetIntFromItAcrossLogicalRecdIndentBlock(BinaryReader binReader, int remainSpace, int length) {
            string str = GetSStrFromItAcrossLogicalRecdIndentBlock(binReader, remainSpace, length);
            return int.Parse(str);
        }

        public static string GetStrFromVariableCharArray(BinaryReader binReader) {
            string tempStr = "";
            while (binReader.PeekChar() != '~') {
                tempStr += binReader.ReadChar();
            }
            binReader.BaseStream.Position += 1;//跳过用于表示时间结束的"~"
            return tempStr;
        }

        public static void SetLogicalRecordIndentBlock(BinaryReader binReader) {
            LogicalRecordIndentBlock logicalRecordIndentBlock = new LogicalRecordIndentBlock();
            logicalRecordIndentBlock.ID = int.Parse(new string(binReader.ReadChars(6)));
            logicalRecordIndentBlock.TypeCode = binReader.ReadChar();
            logicalRecordIndentBlock.ContinuationCode = binReader.ReadChar();
            LogicalRecordIndentBlocks.Add(logicalRecordIndentBlock);
        }
    }

    public class LogicalRecordIndentBlock {
        public int  ID       { get; set; }
        public char TypeCode { get; set; }
        public char ContinuationCode { get; set; }
    }
}
